﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using XProject.DataVisit.Model;
using XProject.DataVisit.Factory;
using XProject.DataVisit.BLL;

namespace XProject.DataVisit.Handler.View.BaseManager.PersonManager
{
    public class PersonView:AbstractView
    {
        private UserInfo CurrentUser;
        public override void init()
        {
            if (HttpContext.Current.Session["UserInfo"] != null)
            {
                CurrentUser = HttpContext.Current.Session["UserInfo"] as UserInfo;
                base.init();
            }
            else
            {
                return;
            }
        }

        public void GetCurrentUser()
        {
            CurrentUser.RoleName = DaoFactory.RoleDao.GetById(CurrentUser.RoleID).RoleName;
            JsonResponse.ResponseObject = CurrentUser;
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        public void GetPersonList()
        {
            IList<UserInfo> list = DaoFactory.UserInfoDao.GetListData();
            IList<UserInfo> list1 = new List<UserInfo>();
            foreach(var k in list)
            {
                if (k.RoleID != Guid.Empty)
                {
                    k.RoleName = DaoFactory.RoleDao.GetById(k.RoleID).RoleName;
                }
                list1.Add(k);
            }
            JsonResponse.ResponseObject = list1;
        }

        public void AddPerson()
        {
            var user = JsonRequest.GetParam<UserInfo>("UserInfo");
            user.LastUpdate = DateTime.Now;
            user.UserPass = PublicFn.getMD5("123456");
            JsonResponse.ResponseObject = DaoFactory.UserInfoDao.Save(user);
        }

        public void UpdatePerson()
        {
            var user = JsonRequest.GetParam<UserInfo>("UserInfo");
            var cu = DaoFactory.UserInfoDao.GetById(user.PID);
            user.LastUpdate = cu.LastUpdate;
            user.UserPass = cu.UserPass;
            user.Job = cu.Job;
            JsonResponse.ResponseObject = DaoFactory.UserInfoDao.Update(user);
        }

        public void DelPerson()
        {
            var user = JsonRequest.GetParam<string>("PID");
            var cu = DaoFactory.UserInfoDao.GetById(new Guid(user));
            JsonResponse.ResponseObject = DaoFactory.UserInfoDao.Delete(cu);
        }

        public void UpdatePass()
        {
            var oldpass = JsonRequest.GetParam<string>("OldPass");
            var newpass = JsonRequest.GetParam<string>("NewPass");

            var cu = DaoFactory.UserInfoDao.GetByNameAndPass(CurrentUser.UserAccount,oldpass);
            if (cu != null)
            {
                cu.UserPass = PublicFn.getMD5(newpass);
            }
            JsonResponse.ResponseObject= DaoFactory.UserInfoDao.Update(cu);
        }

        public void InitPass()
        {
            var user = JsonRequest.GetParam<UserInfo>("UserInfo");
            user.UserPass = PublicFn.getMD5("123456");
            JsonResponse.ResponseObject = DaoFactory.UserInfoDao.Update(user);
        }
    }
}
